﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Subgurim.Controles;
using System.IO;
using HousingInfo.Library;

namespace WebHousingInfo
{
    public partial class TinNhaDatChiTiet : System.Web.UI.Page
    {
        private static string modesave = null, modeid = null, modesavecomplete = null;
        NewsSaveBO newsaveBO;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //tu dong xoa tin het ha
                TuDongXoaTinHetHan();
                //Tu dong xoa thong tin dang ky quang cao
                NewsHouseBOList.XoaQuangCao_khikhongNhantin_sau_ngay(Convert.ToInt32(OptionBO.GetNameOptionBO("ngayxoavitriquangcao").OptionValue));
                modesave = Request.QueryString["save"];
                modeid = Request.QueryString["id"];
                modesavecomplete = Request.QueryString["action"];
                NewsHouseBO nhbo;
                nhbo = NewsHouseBO.GetNewsHouseBO(Convert.ToInt32(modeid));
                nhbo.ViewCount += 1;
                try
                {
                    nhbo.Save();
                }
                catch { }
                if (modesavecomplete == "complete")
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Đã lưu tin !')", true);
                }
                if (modesave != null)
                {
                    if (Session["UserName"] == null)
                        Response.Redirect("DangNhap.aspx");
                    if (NewsSaveBOList.CheckVip_Exits(Convert.ToInt32(modesave)) == false)
                    {
                        newsaveBO = NewsSaveBO.NewNewsSaveBO();
                        newsaveBO.NewsHouseID = Convert.ToInt32(modesave);
                        newsaveBO.UserName = Session["UserName"].ToString();
                        try
                        {
                            newsaveBO.Save();
                            Response.Redirect("TinNhaDatChiTiet.aspx?id=" + modeid + "&action=complete");
                        }
                        catch { }
                    }
                    else ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Đã lưu tin này !')", true);
                }
            }
        }

        public void ShowMap(string street, string city, string country)
        {
            try
            {
                string fulladdress = string.Format("{0}.{1}.{2}", street, city, country);
                string skey = ConfigurationManager.AppSettings["googlemaps.subgurim.net"];
                GeoCode geocode;
                geocode = GMap1.getGeoCodeRequest(fulladdress);
                var glatlng = new Subgurim.Controles.GLatLng(geocode.Placemark.coordinates.lat, geocode.Placemark.coordinates.lng);
                GMap1.setCenter(glatlng, 16, Subgurim.Controles.GMapType.GTypes.Normal);
                var oMarker = new Subgurim.Controles.GMarker(glatlng);
                GMap1.addGMarker(oMarker);
            }
            catch { }
        }

        public bool checkurlimage(string imagestring)
        {
            var pathFileImage = Server.MapPath("TotalUpload/ImagesTotal/" + imagestring);
            if (File.Exists(pathFileImage))
            {
                return true;
            }
            else return false;
        }

        public void TuDongXoaTinHetHan()
        {
            NewsHouseBO newshouseBo;
            var result = NewsHouseBOList.TuDongXoa_TinHetHan_TheoSoNGay(OptionBO.GetNameOptionBO("tudongxoatinhethan").OptionValue);
            foreach (var item in result)
            {
                NewsHouseBOList.Delete_ByKey(item.NewsHouseID);
                newshouseBo = NewsHouseBO.GetNewsHouseBO(item.NewsHouseID);
                if (newshouseBo.UrlImage != "")
                {
                    var array_listImage = newshouseBo.UrlImage.Split('/');
                    foreach (var detail in array_listImage)
                    {
                        deleteExitsImage(detail);
                    }
                }
                NewsHouseBO.DeleteNewsHouseBO(item.NewsHouseID);
            }
        }

        private void deleteExitsImage(string filedelete)
        {
            try
            {
                var pathFileImage = Server.MapPath("TotalUpload/ImagesTotal/" + filedelete);
                FileInfo fileInfo;
                if (File.Exists(pathFileImage))
                {
                    fileInfo = new FileInfo(pathFileImage);
                    fileInfo.Delete();
                }
            }
            catch
            {
            }
        }

    }
}
